package com.example.opengldemo.mirror;

import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.ShaderBuffer;
import com.example.opengldemo.util.ShaderHandle;
import com.example.opengldemo.util.ShaderUtil;

public class Mirror {
    ShaderHandle shaderHandle ;
    ShaderBuffer shaderBuffer ;

    String vertexPath = "mirror/vertex_mirror.sh" ;
    String fragPath = "mirror/fragment_mirror.sh" ;

    final float UNIT_SIZE = 20f ;

    public Mirror(View view){
        String vertexRes = ShaderUtil.loadFromAssetsFile(vertexPath , view.getResources());
        String fragRes = ShaderUtil.loadFromAssetsFile(fragPath , view.getResources());
        int program = ShaderUtil.createProgram(vertexRes , fragRes);
        shaderHandle = new ShaderHandle(program);
        shaderBuffer = new ShaderBuffer();

        shaderBuffer.setVertexBuffer(vertexArray);
        shaderBuffer.setTexcoorBuffer(texcoorsArray);
        shaderBuffer.vCount = vertexArray.length/3 ;

    }
    float[] vertexArray = new float[]{
            -UNIT_SIZE , UNIT_SIZE , 0f ,
            -UNIT_SIZE , -UNIT_SIZE , 0 ,
            UNIT_SIZE , UNIT_SIZE , 0 ,
            UNIT_SIZE , UNIT_SIZE , 0 ,
            -UNIT_SIZE , -UNIT_SIZE , 0 ,
            UNIT_SIZE , -UNIT_SIZE , 0
    };


    float[] texcoorsArray = new float[]{
            0 ,0 ,
            0, 0.8f ,
            1f, 0 ,
            1f, 0 ,
            0, 0.8f ,
            1f ,0.8f
    };

    public void drawSelf(int texture) {
        shaderHandle.program()
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindMVPMaxtrixHandle()
                .bindDefaultTextureHandle(texture)
                .run(GLES30.GL_TRIANGLES ,  shaderBuffer.vCount);
    }
}
